import {ResponseData} from "../src/api/type";
import {tokenData, userData} from "../src/api/user/type";

function createUserList(): Array<userData> {
    return [
        {
            userId: 1,
            avatar: 'https://avatars1.githubusercontent.com/u/554',
            username: 'admin',
            password: '111111',
            desc: '平台管理员',
            roles: ['平台管理员'],
            buttons: ['cuser.detail'],
            routes: ['home'],
            token: 'Admin Token'
        },
        {
            userId: 2,
            avatar: 'https://avatars1.githubusercontent.com/u/554',
            username: 'system',
            password: '111111',
            desc: '系统管理员',
            roles: ['系统管理员'],
            buttons: ['cuser.detail', 'cuser.user'],
            routes: ['home'],
            token: 'System Token'
        }
    ]
}

// 默认导出 Mock 接口配置数组
export default [
    {
        url: '/api/user/login',
        method: 'post',
        response: ({body}): ResponseData<tokenData> => {
            const {username, password} = body
            const checkUser:userData = createUserList().find(
                (item) => item.username === username && password === item.password
            )
            if (!checkUser) {
                return {
                    data: undefined,
                    code: 201,
                    message: '账号或密码错误'
                }
            }
            const {token} = checkUser
            return {
                code: 200,
                data: {token},
                message: '登录成功'
            }
        },
    },
    {
        url: '/api/user/info',
        method: 'get',
        response: (request): ResponseData<userData> => {
            const {token} = request.headers
            const checkUser = createUserList().find((item) => item.token === token)
            if (!checkUser) {
                return {
                    data: undefined,
                    code: 201,
                    message: '获取用户信息失败，请重新登录'
                }
            }
            return {
                code: 200,
                data: checkUser,
                message: '成功'
            }
        },
    }
]